package zuk.clipvietfun.clip;

import java.util.ArrayList;
import java.util.List;

import zuk.clipvietfun.R;
import zuk.clipvietfun.likeandhistory.History;
import zuk.clipvietfun.likeandhistory.Like;
import zuk.clipvietfun.loadimageview.ImageLoader;
import android.content.Context;
import android.text.Html;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class ClipAdapter extends ArrayAdapter<Clip>{
	ImageLoader imageLoader;
	private List<Clip> clips;
	private List<Clip> tempList = new ArrayList<Clip>();
	private List<Clip> listClip;
	private boolean mHasMoreItems;
	int i = 2;
	private int mLastOffset = 0;
	private int LIST_SIZE;

	public List<Clip> getClips() {
		return clips;
	}

	public ClipAdapter(Context context, int textViewResourceId,
			List<Clip> clipsub, List<Clip> listClip, int mLastOffset) {
		super(context, textViewResourceId, clipsub);

		this.clips = clipsub;
		this.listClip = listClip;
		this.imageLoader = new ImageLoader(context);
		this.mLastOffset = mLastOffset;
		this.LIST_SIZE = listClip.size();
		this.mHasMoreItems = true;

	}

	public int getmLastOffset() {
		return mLastOffset;
	}

	public void setmLastOffset(int mLastOffset) {
		this.mLastOffset = mLastOffset;
	}
	public void notifyNoMoreItems(){
		mHasMoreItems = false;
	}
	@SuppressWarnings("unchecked")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;


		if (position == getCount() - 1 && mHasMoreItems) {
			tempList.clear();
			int lastOffset = getmLastOffset();
			if (lastOffset < LIST_SIZE) {
				int limit = lastOffset + 20;
				for (int i = (lastOffset + 1); (i <= limit && i < LIST_SIZE); i++) {
					tempList.add(listClip.get(i));
				}
				setmLastOffset(limit);
				if (limit < LIST_SIZE) {
					mHasMoreItems = true;
				}
				else{
					mHasMoreItems = false;
				}
				new LoadClipEnlessAdapterTask(getContext(), this).execute(tempList);
			}
		}
		if (v == null) {
			LayoutInflater infalInflater = (LayoutInflater) getContext()
			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = infalInflater.inflate(R.layout.clip, null);
		}
		Clip clip = clips.get(position);

		TextView tViewTitle = (TextView)v.findViewById(R.id.text_title);
		tViewTitle.setText(Html.fromHtml(clip.getTitle()));

		ImageView tick = (ImageView)v.findViewById(R.id.tick);
		tick.setVisibility(View.INVISIBLE);
		if(new History(getContext()).isHas(clip)){
			tick.setVisibility(View.VISIBLE);
		}else {
			tick.setVisibility(View.INVISIBLE);
		}
		
		ImageView like = (ImageView)v.findViewById(R.id.like);
		like.setVisibility(View.INVISIBLE);
		if(new Like(getContext()).isHas(clip)){
			like.setVisibility(View.VISIBLE);
		}else {
			like.setVisibility(View.INVISIBLE);
		}
		

		boolean isCrop = false;
		RelativeLayout ly_thumb = (RelativeLayout)v.findViewById(R.id.ly_thumb);
		android.view.ViewGroup.LayoutParams params = ly_thumb.getLayoutParams();
		DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
		if(metrics.densityDpi != 160 && metrics.widthPixels <= 480){
			Log.e("DPI!!!!!!!!!", Integer.toString(metrics.densityDpi));
			params.width = (int)(metrics.widthPixels/2.6);
			isCrop = true;
		}else {
			params.width =(int)(200*metrics.density);
			isCrop = false;
		}

		ImageView img_thumb = (ImageView)v.findViewById(R.id.img_thumb);
		ProgressBar prgBar = (ProgressBar)v.findViewById(R.id.prgBar);
		img_thumb.setVisibility(View.GONE);
		String imgurl = "http://img.youtube.com/vi/"+clip.getuTube()+"/mqdefault.jpg";

		imageLoader.DisplayImage(imgurl, img_thumb, prgBar, isCrop);
		return v;
	}


}
